package beautiful.butterfly.drds.schedule.task;

public class LockObject {
    private int threadCount = 0;
    private Object object = new Object();

    public LockObject() {
    }

    public void waitCurrentThread() throws Exception {
        this.object.wait();

    }

    public void notifyOtherThread() throws Exception {
        synchronized (object) {
            this.object.notifyAll();
        }
    }

    public void addThread() {
        synchronized (this) {
            threadCount = threadCount + 1;
        }
    }

    public void realseThread() {
        synchronized (this) {
            threadCount = threadCount - 1;
        }
    }

    public boolean realseThreadButNotLast() {
        synchronized (this) {
            if (this.threadCount == 1) {
                return false;
            } else {
                threadCount = threadCount - 1;
                return true;
            }
        }
    }

    public int count() {
        synchronized (this) {
            return threadCount;
        }
    }
}
